package com.gwz.service.taskService;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gwz.mapper.NewsMapper;
import com.gwz.model.NewsModel;
import com.gwz.model.NewsVo;
import com.gwz.util.HtmlUtil;

/**
 * 抓取A5官网最新源码代码业务service
 * @author Administrator
 *
 */
@Service("a5TaskServiceImp")
public class A5TaskServiceImp implements TaskService{

	private static Logger logger = Logger.getLogger(A5TaskServiceImp.class);
	
	private static String a5Domain="http://down.admin5.com";

	private static String a5="A5";
	
	@Autowired
	private NewsMapper newsMapper;
	
	/**
	 * 业务入口
	 */
	public void doTask(String extendParams) {
			A5News(extendParams);
	}

	/**
	 * 解析A5官网首页
	 * @param extendParams
	 */
	private void A5News(String extendParams){
        Document doc,docCh;
        List<Node> nodeList = null;
        Node node=null;
        Element aEle;
        String dateStr="";
        Element masthead =null;
        Elements list = null;
        NewsModel model = null;
		try {
			doc = Jsoup.connect(a5Domain).userAgent("Mozilla").timeout(3000).get();
			masthead = doc.select("div#xxkbody2 ul").first();
			list = masthead.getElementsByTag("li");
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
		NewsVo news=null;
      //遍历标签
        for (Element link : list) {
        	nodeList=link.childNode(0).childNodes();
        	if(nodeList==null || nodeList.size()<2) {
        		continue;
        	}
        	node=nodeList.get(0);
    		node=nodeList.get(1);
    		dateStr=node.toString();
    		if(!dateStr.contains("#ff0000")) {
    			continue;
    		}
    		aEle=link.select("a[href]").get(1);
    		model = new NewsModel();
    		model.setType(a5);
    		try {
    			docCh = Jsoup.connect(a5Domain+aEle.attr("href")).userAgent("Mozilla").timeout(3000).get();
    			model.setTitle((docCh.title()).replace(a5, StringUtils.EMPTY));
    			model.setDescription(docCh.select("meta[name=description]").get(0).attr("content"));
    			model.setKey(docCh.select("meta[name=keywords]").get(0).attr("content"));
    			model.setContent(docCh.select("div.con_text").first().html());
    			model.setFrom(1);
    			model.setContent(model.getContent().replace("onclick", "onclicknodo"));
    			news=newsMapper.getNewsVoByTitleAndType(model.getTitle(), a5);
    			if(news==null) {
    				HtmlUtil.createNewsHtml(extendParams,model,a5Domain);
    				newsMapper.saveNews(model);
    			}
    		} catch (IOException e) {
    			logger.error(e);
    		}
        }
    }
	
}
